package qs.openxt.libs.remoting;

import android.content.Context;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import qs.openxt.libs.R;
import qs.openxt.libs.progress.UploadDialogProgress;
import qs.openxt.libs.progress.UploadDialogProgress.OnUploadDialogProgressListener;
import qs.openxt.libs.util.ActivityUtil;
import qs.openxt.libs.util.PictureUtil;

public class Upload implements OnUploadDialogProgressListener {
	protected Context context = null;
	protected OnActionListener listener = null;
	protected UploadDialogProgress dialogProgress = null;

	public Upload(Context context, OnActionListener l) {
		this.context = context;
		this.listener = l;

		dialogProgress = new UploadDialogProgress(context, this);
	}

	public void doRequest(JSONObject packet) {
		if (!ActivityUtil.isNetworkAvailable(context)) {
			Toast.makeText(context, context.getString(R.string.isNetworkAvailable), Toast.LENGTH_SHORT).show();
			return;
		}

		dialogProgress.show();
		try {
			/*********XUtils 上传文件********/
			String url = packet.getString("url");
			JSONObject param = packet.getJSONObject("param");

			RequestParams params = new RequestParams();
			if (param != null && param.size() > 0) {
				Iterator it = param.keySet().iterator();
				while (it.hasNext()) {
					String key = (String) it.next();
					params.addBodyParameter(key, param.getString(key));
				}
			}

			Object obj = packet.get("file");
			if(obj instanceof String){
				String filePath = (String) obj;
				String path = PictureUtil.getFilePath(filePath);
				params.addBodyParameter("upfile", new File(path));
			}else if(obj instanceof JSONArray){
				JSONArray files = (JSONArray) obj;
				for(int i = 0, j = files.size(); i < j; i++){
					String path = PictureUtil.getFilePath(files.getString(i));
					params.addBodyParameter("upfile", new File(path));
				}
			}else if(obj instanceof List){
				List files = (List) obj;
				for(int i = 0, j = files.size(); i < j; i++){
					String path = PictureUtil.getFilePath(files.get(i).toString());
					params.addBodyParameter("upfile", new File(path));
				}
			}
			HttpUtils http = new HttpUtils(5 * 60 * 1000);
			http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
				@Override
				public void onStart() {
				}

				@Override
				public void onLoading(long total, long current, boolean isUploading) {
					if (isUploading) {
						onProgress((int) ((current / (float) total) * 100));
					} else {
						onProgress((int) ((current / (float) total) * 100));
					}
				}

				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					doSuccess(responseInfo.result);
				}

				@Override
				public void onFailure(HttpException error, String msg) {
					doError();
				}
			});
			/*********XUtils 上传文件********/
		} catch (Exception e) {
			dialogProgress.close();
		}
	}

	public void onProgress(int progress) {
		dialogProgress.setProgress(progress);
	}

	public void doSuccess(String reVal) {
		dialogProgress.close();

		if (listener != null) {
			listener.onActionCompleted(reVal);
		}
	}
	
	public void doError() {
		dialogProgress.close();

		if (listener != null) {
			listener.onActionError();
		}
	}

	public interface OnActionListener {
		public void onActionCompleted(String reVal);
		public void onActionError();
	}

	@Override
	public void onCancel() {

	}
}
